package io.renren.modules.mybog.fromControoler.utiles;

import com.google.gson.Gson;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * 获取qq信息类
 */
@Component
public class QQ {



    public static   HashMap getNickname (String qq)   {
        InputStream is = null;
        String body = null;
        StringBuilder   res=new StringBuilder();
        HashMap returnMap = new HashMap<>();
        String url=qq;
        HttpGet get = new HttpGet(url);
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try{
            response = client.execute(get);
            RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();
            get.setConfig(config);
            HttpEntity entity = response.getEntity();

            if(entity != null){
                is = entity.getContent();
                //转换为字节输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));

                while((body=br.readLine()) != null){
                    res.append(body);
                }
            }
            Gson gson = new Gson();
            //通过gson吧string类型转成HashMap集合
            returnMap = gson.fromJson(res.toString(),HashMap.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnMap;
    }
}
